package com.aidex.biz.smy.utils;

import lombok.SneakyThrows;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class MapUtils {

    /**
     * 对象转化为 Map
     *
     * @param obj 要转换的对象
     * @return map 转换结果
     * @throws IllegalAccessException 访问权限的异常
     */
    @SneakyThrows
    public static Map<String, String> objectToMap(Object obj) {
        if (obj == null) {
            return null;
        }
        Map<String, String> map = new HashMap<>(16);
        Field[] declaredFields = obj.getClass().getDeclaredFields();
        for (Field field : declaredFields) {
            field.setAccessible(true);
            if (null != field.get(obj)) {
                map.put(field.getName(), (String) field.get(obj));
            }
        }
        return map;
    }


}
